/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.stats;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.*;

public class RecordBufferThread extends Thread {
    private Socket sock;

    private Vector<String> records = new Vector<String>();

    private int sensorID = -1;

    public RecordBufferThread(int sensorID, Socket sock) {
        this.sensorID = sensorID;
        this.sock = sock;
    }

    public void run() {
        DataInputStream dis = null;
        DataOutputStream dos = null;

        try {
            dis = new DataInputStream(sock.getInputStream());
            dos = new DataOutputStream(sock.getOutputStream());
            dos.writeInt(sensorID);
            long count = 0;
            while (true) {
                String rec = nextRecord();
                if (rec == null) {
                    Thread.sleep(1000);
                } else {
                    byte[] bytes = rec.getBytes();
                    dos.writeInt(bytes.length);
                    dos.write(bytes);
                    count++;
                    if (count % 1000 == 0) 
                        System.out.println("RecordBuffer: processed " + count + " records");
                }
            }

        } catch (Exception E) {
            E.printStackTrace();
        } finally {
            
            try {
                dis.close();
            } catch (Exception e) {
                // no op
            }
            try {
                dos.close();
            } catch (Exception e) {
                // no op
            }
            try {
                sock.close();
            } catch (Exception e) {
                // no op
            }
        }
    }

    public synchronized void addRecord(String rec) {
        records.addElement(rec);
    }

    public synchronized String nextRecord() {
        if (records.size() == 0)
            return null;
        return (String) records.remove(0);
    }

    public synchronized int size() {
        return records.size();
    }

}
